package com.rn.ddd.infrastructure.repository.converter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rn.ddd.domain.merchant.entity.Merchant;
import com.rn.ddd.infrastructure.base.BaseDataConverter;
import com.rn.ddd.infrastructure.repository.po.MerchantPO;
import lombok.extern.slf4j.Slf4j;

/**
 * 商户数据转换器
 *
 * @author 然诺
 * @date 2021/8/15
 */
@Slf4j
public class MerchantDataConverter extends BaseDataConverter {

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    public static MerchantPO toData(Merchant merchant) {
        MerchantPO po = convert(merchant, MerchantPO.class);
        try {
            po.setRatesJson(OBJECT_MAPPER.writeValueAsString(merchant.getRates()));
        } catch (JsonProcessingException e) {
            log.error("toData -> convert rates to json error", e);
            return null;
        }
        return po;
    }

}
